﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudyCode.ORM.Entities.Attributes;
using StudyCode.ORM.Repository;

namespace StudyCode.ORM.Entities
{
    public class Book : Entity, IEntityBuild
    {
        [PrimaryKey]
        public override long Id { get; set; }

        public string Name { get; set; }

        public long AuthorId { get; set; }

        [ForeignerKey("AuthorId")]
        public Author Author { get; set; }

        public string Number { get; set; }

        public decimal Price { get; set; }

        public DateTime BugTime { get; set; }

        public int Count { get; set; }

        public string EntityToSqlFromPrimaryKey()
        {
            return SqlBuilder.GetComplex(GetType(),Id);
        }

        /// <summary>
        /// todo 扩展到可以自动的生成
        /// </summary>
        /// <param name="reader"></param>
        public void SqlToEntity(SqlDataReader reader)
        {
            Author = new Author();
            Id = (long)reader["Book_Id"];
            Author.Id = (long)reader["Book_AuthorId"];
            Author.Name = (string)reader["Author_Name"];
            //todo other attribute
        }
    }
}
